package com.ruoyi.information.controller.pc;

import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.information.domain.pc.Collection;
import com.ruoyi.information.service.pc.ICollectionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 收藏Controller
 *
 * @author ruoyi
 * @date 2023-06-13
 */
@RestController
@RequestMapping("/collection")
public class CollectionController extends BaseController {
    @Autowired
    private ICollectionService collectionService;

    /**
     * 查询收藏列表
     */
    @GetMapping("/list")
    public AjaxResult list(Collection collection) {
        AjaxResult result = new AjaxResult();
        List<Object> objectList = collectionService.selectCollectionList(collection);
        if (objectList != null && !objectList.isEmpty()) {
            startPage();
            List<Object> list = collectionService.selectCollectionList(collection);
            result.put("rows",list);
            result.put("msg","操作成功");
            result.put("code",200);
            result.put("total",objectList.size());
        }else {
            result.put("rows",null);
            result.put("msg","操作成功");
            result.put("code",200);
            result.put("total",0);
        }

        return result;
    }

    /**
     * 查询收藏列表
     */
    @GetMapping("/app")
    public AjaxResult app(Collection collection) {
        startPage();
        List<Object> list = collectionService.selectCollectionList(collection);
        return success(list);
    }


    /**
     * 获取收藏详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(collectionService.selectCollectionById(id));
    }

    /**
     * 新增收藏
     */
    @PostMapping
    public AjaxResult add(@RequestBody Collection collection) {
        if (collection.getCollectionType() != null && collection.getCollectionVal() != null && SecurityUtils.getUserId() != 0) {
            collection.setUserId(SecurityUtils.getUserId());
            return toAjax(collectionService.insertCollection(collection));
        }
        return success();
    }

    /**
     * 取消收藏
     */
    @PutMapping
    public AjaxResult edit(@RequestBody Collection collection) {
        if (collection.getCollectionType() != null && collection.getCollectionVal() != null) {
            collection.setUserId(SecurityUtils.getUserId());
            return toAjax(collectionService.updateCollection(collection));
        } else {
            return error("collectionType或collectionVal没有获取到");
        }
    }

    /**
     * 删除收藏
     */
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(collectionService.deleteCollectionByIds(ids));
    }
}
